package com.baosight.risk.common.workflow;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.service.soa.XLocalManager;
import com.baosight.risk.api.workflow.WorkflowAuthService;
import com.baosight.risk.api.xtzy.IXtzyCompanyService;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.constant.WorkflowConstant;
import com.baosight.risk.common.utils.ShiroUtils;
import com.baosight.risk.mapper.workflow.WorkflowAuthMapper;
import com.baosight.risk.mapper.xtzy.XtzyUserMapper;
import com.baosight.risk.service.workflow.entity.WorkflowAuth;
import com.baosight.risk.service.workflow.vo.WorkflowAuthVO;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import com.baosight.risk.service.xtzy.vo.XtzyCompanyVO;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmActivity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.pvm.process.TransitionImpl;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者： lishaoqing
 * 时间： 2019年06月15日
 * 描述： 工作流管理类,封装了平台接口调用（基于Activiti5,Iplat4j V6）
 * 版本: v1.0
 */
@Service
public class WorkFlowManager {

    @Autowired
    private WorkflowAuthMapper workflowAuthMapper;

    @Autowired
    private XtzyUserMapper xtzyUserMapper;

    @Autowired
    private IXtzyCompanyService companyService;

    @Autowired
    private WorkflowAuthService workflowAuthService;

    private RuntimeService getRuntimeService() {
        return ProcessEngines.getDefaultProcessEngine().getRuntimeService();
    }

    private TaskService getTaskService() {
        return ProcessEngines.getDefaultProcessEngine().getTaskService();
    }

    private RepositoryService getRepositoryService() {
        return ProcessEngines.getDefaultProcessEngine().getRepositoryService();
    }

    /**
     * 根据授权规则启动一个流程
     *
     * @param processKey  流程定义编码（必要参数）
     * @param businessKey 自定义业务id（必要参数）
     * @param variables   自定义全局变量
     */
    public void startProcess(String processKey, String businessKey, Map<String, Object> variables) {
        XtzyUser sysUser = ShiroUtils.getUser();
        String starterId = sysUser.getUserCode();
        EiInfo inInfo = new EiInfo();

        List<WorkflowAuth> auths = workflowAuthMapper.selectAll();
        List<WorkflowAuth> myAuths = new ArrayList<>();

        //根据流程定义编码获取
        ActivityImpl firstUserTask = null;
        for (WorkflowAuth auth : auths) {
            if (auth.getProcessKey().equals(processKey)) {
                firstUserTask = getFirstUserTask(auth.getProcessDefId());
                break;
            }
        }
        //流程定义中至少包含一个人工活动节点
        if (firstUserTask != null) {
            String firstNodekey = firstUserTask.getId();

            //获取当前流程的授权规则
            for (WorkflowAuth auth : auths) {
                if (auth.getProcessKey().equals(processKey)) {
                    //先配置第一个人工活动节点的审批人
                    if (auth.getNodeKey().equals(firstNodekey)) {
                        List<Map<String, Object>> userList = new ArrayList<>();
                        WorkflowAuthVO authVO = convertAuth(auth);
                        List<XtzyUser> users = getUsersByAuth(authVO);
                        for (XtzyUser user : users) {
                            Map<String, Object> nextAuditUser = new HashMap<>();
                            nextAuditUser.put("userId", user.getUserCode());
                            nextAuditUser.put("userName", user.getUserDesc());
                            nextAuditUser.put("deptEname", auth.getCompId());
                            nextAuditUser.put("deptCname", auth.getCompName());
                            nextAuditUser.put("roleEname", auth.getRoleId());
                            nextAuditUser.put("roleCname", auth.getRoleName());
                            userList.add(nextAuditUser);
                        }
                        inInfo.set("nextActivityUserList", userList);
                    } else {
                        myAuths.add(auth);
                    }
                }
            }

            //启动流程实例
            inInfo.set("processDefinitionKey", processKey); //流程定义编码
            inInfo.set("starterId", starterId); //流程启动者ID
            inInfo.set("businessKey", businessKey);
            inInfo.set(EiConstant.serviceName, "EW11");
            inInfo.set(EiConstant.methodName, "startProcess");
            EiInfo outInfo = XLocalManager.call(inInfo);
            String processInstanceId = outInfo.getString("processInstanceId");

            //设置流程实例的全局变量
            if (variables != null && variables.size() > 0 && !StringUtils.isEmpty(processInstanceId)) {
                getRuntimeService().setVariables(processInstanceId, variables);
            }

            //配置其它人工活动节点的审批人
            Object result = outInfo.get("processInstanceId");
            if (result != null) {
                List<String> processInstanceIds = new ArrayList<>();
                if (result instanceof List) {
                    processInstanceIds = (List<String>) result;
                } else {
                    processInstanceIds.add(result.toString());
                }
                for (String pid : processInstanceIds) {
                    addUserList(myAuths, pid);
                }
            }
        }
    }

    /**
     * 批量审核
     *
     * @param comment      审核意见
     * @param businessKeys 业务id（必要参数）
     * @return 接口调用结果
     */
    public int batchSubmit(String comment, List<String> businessKeys, List<XtzyUser> users) {
        XtzyUser sysUser = ShiroUtils.getUser();
        String userCode = sysUser.getUserCode();

        List<Map<String, String>> nextActivityUserList = convertXtzyUser(users);
        List<Map<String, Object>> submitList = new ArrayList<>();

        //查询要提交的任务id
        Map<String, Map<String, String>> queryMap = getInfoListByBusinessKey(sysUser, businessKeys);
        for (String businessKey : businessKeys) {
            String taskId = queryMap.get(businessKey).get(WorkflowConstant.TASK_ID);
            Map<String, Object> map = new HashMap<>();
            Map<String, Object> variables = new HashMap<>();
            map.put("taskId", taskId);
            map.put("userId", userCode);
            map.put("comment", comment);
            map.put("approvalResult", WorkflowConstant.APPROVAL);
            if (nextActivityUserList.size() > 0) {
                map.put("nextActivityUserList", nextActivityUserList);
            }
            map.put("variables", variables);
            submitList.add(map);
        }

        EiInfo inInfo = new EiInfo();
        inInfo.set("list", submitList);
        inInfo.set(EiConstant.serviceName, "EW11");
        inInfo.set(EiConstant.methodName, "batchSubmit");
        EiInfo outInfo = XLocalManager.call(inInfo);
        return outInfo.getStatus();
    }

    /**
     * 批量回退
     *
     * @param comment 回退意见
     * @return 接口调用结果
     */
    public int batchReturns(String comment, List<String> businessKeys) {
        XtzyUser sysUser = ShiroUtils.getUser();
        String userId = sysUser.getUserCode();

        EiInfo inInfo = new EiInfo();
        List<Map<String, Object>> list = new ArrayList<>();

        //查询要提交的任务id
        Map<String, Map<String, String>> queryMap = getInfoListByBusinessKey(sysUser, businessKeys);
        for (String businessKey : businessKeys) {
            String taskId = queryMap.get(businessKey).get(WorkflowConstant.TASK_ID);
            Map<String, Object> map = new HashMap<>();
            map.put("taskId", taskId);
            map.put("userId", userId);
            map.put("comment", comment);
            map.put("approvalResult", WorkflowConstant.DISAPPROVAL);
            list.add(map);
        }

        inInfo.set("list", list);
        inInfo.set(EiConstant.serviceName, "EW11");
        inInfo.set(EiConstant.methodName, "batchReturns");
        EiInfo outInfo = XLocalManager.call(inInfo);
        return outInfo.getStatus();
    }

    /**
     * 批量中止(结束)流程实例
     *
     * @param sysUser      当前用户
     * @param businessKeys 业务键
     * @param variables    全局变量
     * @return 接口调用结果
     */
    public int batchEndProcessesByProcessTask(XtzyUser sysUser, List<String> businessKeys, Map<String, Object> variables) {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Map<String, String>> taskIds = getInfoListByBusinessKey(sysUser, businessKeys);
        for (String businessKey : businessKeys) {
            Map<String, String> info = taskIds.get(businessKey);
            Map<String, Object> map = new HashMap<>();
            map.put("taskId", info.get(WorkflowConstant.TASK_ID));
            map.put("variables", variables);
            list.add(map);
        }
        for (String key : taskIds.keySet()) {

        }

        EiInfo inInfo = new EiInfo();
        inInfo.set("list", list);
        inInfo.set(EiConstant.serviceName, "EW11");
        inInfo.set(EiConstant.methodName, "batchEndProcessesByProcessTask");
        EiInfo outInfo = XLocalManager.call(inInfo);
        return outInfo.getStatus();
    }

    /**
     * 批量删除流程实例
     *
     * @param sysUser      当前用户
     * @param businessKeys 当前用户
     * @return 接口调用结果
     */
    public int batchDeleteProcessInstances(XtzyUser sysUser, List<String> businessKeys) {
        Map<String, Map<String, String>> map = getInfoListByBusinessKey(sysUser, businessKeys);
        List<String> list = new ArrayList<>();
        for (String businessKey : businessKeys) {
            Map<String, String> info = map.get(businessKey);
            list.add(info.get(WorkflowConstant.PROCESS_INSTANCE_ID));
        }
        EiInfo inInfo = new EiInfo();
        inInfo.set("list", list);
        inInfo.set(EiConstant.serviceName, "EW11");
        inInfo.set(EiConstant.methodName, "batchDeleteProcessInstances");
        EiInfo outInfo = XLocalManager.call(inInfo);
        return outInfo.getStatus();
    }

    /**
     * 获取流程实例状态
     *
     * @param processInstanceIds 流程实例id
     * @return 流程实例状态
     */
    public String getProcessInstanceStatus(List<String> processInstanceIds) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (String processInstanceId : processInstanceIds) {
            Map<String, Object> map = new HashMap<>();
            map.put("processInstanceId", processInstanceId);
            list.add(map);
        }
        EiInfo inInfo = new EiInfo();
        inInfo.set("list", list);
        inInfo.set(EiConstant.serviceName, "EW11");
        inInfo.set(EiConstant.methodName, "batchGetProcessInstanceStatus");
        EiInfo outInfo = XLocalManager.call(inInfo);
        return (String) outInfo.get("instStatus");
    }

    /**
     * 设置全局变量
     *
     * @param processInstanceId 流程实例id
     * @param variables         全局变量
     * @return 接口调用结果
     */
    public int setVariable(String processInstanceId, Map<String, Object> variables) {
        EiInfo inInfo = new EiInfo();
        inInfo.set("processInstanceId", processInstanceId);
        inInfo.set("variables", variables);
        inInfo.set(EiConstant.serviceName, "EW11");
        inInfo.set(EiConstant.methodName, "setVariable");
        EiInfo outInfo = XLocalManager.call(inInfo);
        return outInfo.getStatus();
    }

    /**
     * 获取全局变量
     *
     * @param processInstanceId 流程实例id
     * @param variableKey       全局变量key
     * @return 全局变量集合
     */
    public Map<String, Object> getVariables(String processInstanceId, String variableKey) {
        EiInfo inInfo = new EiInfo();
        inInfo.set("processInstanceId", processInstanceId);
        inInfo.set("processVariableKey", variableKey);
        inInfo.set(EiConstant.serviceName, "EW11");
        inInfo.set(EiConstant.methodName, "getVariables");
        EiInfo outInfo = XLocalManager.call(inInfo);
        return (Map<String, Object>) outInfo.get("result");
    }

    /**
     * 获取当前流程定义的所有节点
     *
     * @param processDefinitionId 流程定义id
     * @return 当前流程定义的所有节点
     */
    public List<ActivityImpl> getAllUserTask(String processDefinitionId) {
        ProcessDefinitionEntity def = (ProcessDefinitionEntity) ((RepositoryServiceImpl) getRepositoryService()).getDeployedProcessDefinition(processDefinitionId);
        //获取当前流程定义的所有节点
        List<ActivityImpl> activitiList = def.getActivities();
        return activitiList;
    }

    /**
     * 获取第一个人工活动节点
     *
     * @param processDefinitionId 流程定义id
     * @return 第一个人工活动节点
     */
    private ActivityImpl getFirstUserTask(String processDefinitionId) {
        List<ActivityImpl> activitiList = getAllUserTask(processDefinitionId);

        //获取第一个可配置的人工活动节点
        List<ActivityImpl> list = new ArrayList<>();
        for (ActivityImpl activity : activitiList) {
            Map<String, Object> properties = activity.getProperties();
            if (properties.get("type").equals(WorkflowConstant.NODETYPE_START)) {
                findFirstUserTask(list, activity);
                break;
            }
        }
        if (list.size() == 1) { //目前仅支持单分支
            return list.get(0);
        }
        return null;
    }

    /**
     * 根据流程定义id获取下一步审批人
     *
     * @param sysUser     当前用户
     * @param businessKey 业务id
     * @return 下一步审批人
     */
    public List<XtzyUser> findNextUsers(XtzyUser sysUser, String businessKey) {
        Map<String, String> queryMap = getInfoByBusinessKey(sysUser, businessKey);
        //流程定义编码
        String processKey = queryMap.get(WorkflowConstant.PROCESS_DEFINITION_KEY);
        //当前节点
        String currentNodeKey = queryMap.get(WorkflowConstant.CURRENT_NODE_KEY);
        //流程定义id
        String processDefinitionId = queryMap.get(WorkflowConstant.PROCESS_DEFINITION_ID);
        //流程定义的所有节点
        List<ActivityImpl> activitiList = getAllUserTask(processDefinitionId);
        //查找下一个节点
        Map<String, Object> map = new HashMap<>();
        map.put("activitiList", activitiList);
        map.put("currentNodeKey", currentNodeKey);
        for (ActivityImpl activity : activitiList) {
            Map<String, Object> properties = activity.getProperties();
            if (properties.get("type").equals(WorkflowConstant.NODETYPE_START)) {
                map.put("findActivity", activity);
                map.put("flag", false);
                recursionActivity(map);
                break;
            }
        }

        ActivityImpl nextActivity = (ActivityImpl) map.get("target");
        //下一步是结束节点
        if (nextActivity.getProperties().get("type").equals(WorkflowConstant.NODETYPE_END)) {
            return null;
        }

        String nextNodeKey = nextActivity.getId();
        WorkflowAuthVO auth = workflowAuthService.selectAuthByProcessKey(processKey, nextNodeKey);
        List<XtzyUser> users = getUsersByAuth(auth);
        return users != null ? users : new ArrayList<>();
    }

    /**
     * 递归查找下一个节点
     *
     * @param map 包含出入参的map
     */
    private void recursionActivity(Map<String, Object> map) {
        ActivityImpl findActivity = (ActivityImpl) map.get("findActivity");
        String currentNodeKey = (String) map.get("currentNodeKey");

        if (map.containsKey("currentActivity")) {
            //目前仅支持没有分支的流程
            ActivityImpl currentActivity = (ActivityImpl) map.get("currentActivity");
            PvmActivity destination = currentActivity.getOutgoingTransitions().get(0).getDestination();
            map.put("target", currentActivity.getOutgoingTransitions().get(0).getDestination());
        } else {
            List<PvmTransition> outgoingTransitions = findActivity.getOutgoingTransitions();

            for (PvmTransition outgoingTransition : outgoingTransitions) {
                if (map.containsKey("target")) {
                    break;
                }
                TransitionImpl t = (TransitionImpl) outgoingTransition;
                ActivityImpl a = t.getDestination();
                if (a.getId().equals(currentNodeKey)) {
                    map.put("currentActivity", a);
                }
                map.put("findActivity", a);
                recursionActivity(map);
            }
        }

    }

    /**
     * 递归查找第一个人工活动节点（可能有多个分支）
     *
     * @param list        查找结果
     * @param destination 当前节点
     */
    private void findFirstUserTask(List<ActivityImpl> list, ActivityImpl destination) {
        if (destination.getProperties().get("type").equals(WorkflowConstant.NODETYPE_USERTASK)) {
            list.add(destination);
        } else {
            List<PvmTransition> outgoingTransitions = destination.getOutgoingTransitions();
            for (PvmTransition outgoingTransition : outgoingTransitions) {
                TransitionImpl t = (TransitionImpl) outgoingTransition;
                ActivityImpl a = t.getDestination();
                findFirstUserTask(list, a);
            }
        }
    }

    /**
     * 为每个人工活动节点配置审批人
     *
     * @param myAuths           流程授权规则
     * @param processInstanceId 流程实例id
     */
    public void addUserList(List<WorkflowAuth> myAuths, String processInstanceId) {
        //缓存数据库查询结果,减少查询次数
        Map<String, List<XtzyUser>> userMap = new HashMap<>();

        for (WorkflowAuth auth : myAuths) {
            List<XtzyUser> users = null;
            String userkey = auth.getCompId() + auth.getRoleId();
            if (userMap.containsKey(userkey)) {
                users = userMap.get(userkey);
            } else {
                users = xtzyUserMapper.getRoleByRoleIdAndCompId(auth.getCompId(), auth.getRoleId());
                userMap.put(auth.getCompId() + auth.getRoleId(), users);
            }
            //分配审批人
            List<Map<String, String>> userList = new ArrayList<>();
            for (XtzyUser user : users) {
                Map<String, String> nextAuditUser = new HashMap<>();
                nextAuditUser.put("userId", user.getUserCode());
                nextAuditUser.put("userName", user.getUserDesc());
                nextAuditUser.put("deptEname", auth.getCompId());
                nextAuditUser.put("deptCname", auth.getCompName());
                nextAuditUser.put("roleEname", auth.getRoleId());
                nextAuditUser.put("roleCname", auth.getRoleName());
                userList.add(nextAuditUser);
            }
            EiInfo inInfo = new EiInfo();
            inInfo.set("processInstanceId", processInstanceId);
            inInfo.set("ManualKey", auth.getNodeKey());
            inInfo.set("userList", userList);
            inInfo.set(EiConstant.serviceName, "EW11");
            inInfo.set(EiConstant.methodName, "setProcessVariables");
            XLocalManager.call(inInfo);
        }
    }

    /**
     * 获取用户的所有待办
     *
     * @return 待办任务
     */
    public List<Task> getTaskList(XtzyUser sysUser) {
        String userCode = sysUser.getUserCode();
        List<Task> taskList = getTaskService().createTaskQuery().taskAssignee(userCode).list();
        return taskList;
    }

    /**
     * 获取用户所有待办中的业务键
     *
     * @return 待办中的业务键
     */
    public List<String> getTaskbusinessKeys(XtzyUser sysUser, List<Task> taskList) {
        String userCode = sysUser.getUserCode();
        List<String> keys = new ArrayList<>();

        if (taskList == null) {
            taskList = getTaskService().createTaskQuery().taskAssignee(userCode).list();
        }

        for (Task task : taskList) {
            String processInstanceId = task.getProcessInstanceId();
            ProcessInstance pi = getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
            //使用流程实例ID查询业务键
            String businessKey = pi.getBusinessKey();
            if (StringUtils.isNotEmpty(businessKey)) {
                keys.add(businessKey);
            }
        }
        return keys;
    }

    /**
     * 根据业务id获取当前节点编码
     *
     * @param sysUser     当前用户
     * @param businessKey 业务id
     * @return Map 查询结果
     */
    public Map<String, String> getInfoByBusinessKey(XtzyUser sysUser, String businessKey) {
        Map<String, String> result = new HashMap<>();
        List<Task> taskList = getTaskList(sysUser);

        for (Task task : taskList) {
            String processInstanceId = task.getProcessInstanceId();
            //使用流程实例ID查询业务键
            ProcessInstance pi = getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
            String key = pi.getBusinessKey();
            if (StringUtils.isNotEmpty(key) && key.equals(businessKey)) {
                result.put(WorkflowConstant.PROCESS_INSTANCE_ID, processInstanceId);
                result.put(WorkflowConstant.PROCESS_DEFINITION_KEY, pi.getProcessDefinitionKey());
                result.put(WorkflowConstant.TASK_ID, task.getId());
                result.put(WorkflowConstant.CURRENT_NODE_KEY, task.getTaskDefinitionKey());
                result.put(WorkflowConstant.PROCESS_DEFINITION_ID, pi.getProcessDefinitionId());
                break;
            }
        }
        return result;
    }

    /**
     * 根据业务id获取流程定义编码,流程实例id,任务id
     *
     * @param sysUser      当前用户
     * @param businessKeys 业务id
     * @return Map 查询结果
     */
    public Map<String, Map<String, String>> getInfoListByBusinessKey(XtzyUser sysUser, List<String> businessKeys) {
        Map<String, Map<String, String>> result = new HashMap<>();
        List<Task> taskList = getTaskList(sysUser);

        for (Task task : taskList) {
            String processInstanceId = task.getProcessInstanceId();
            String processDefinitionId = task.getProcessDefinitionId();
            //使用流程实例ID查询业务键
            ProcessInstance pi = getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
            String key = pi.getBusinessKey();
            if (StringUtils.isNotEmpty(key)) {
                for (String businessKey : businessKeys) {
                    if (key.equals(businessKey)) {
                        Map<String, String> map = new HashMap<>();
                        map.put(WorkflowConstant.PROCESS_INSTANCE_ID, processInstanceId);
                        map.put(WorkflowConstant.PROCESS_DEFINITION_KEY, pi.getProcessDefinitionKey());
                        map.put(WorkflowConstant.TASK_ID, task.getId());
                        map.put(WorkflowConstant.CURRENT_NODE_KEY, task.getTaskDefinitionKey());
                        map.put(WorkflowConstant.PROCESS_DEFINITION_ID, pi.getProcessDefinitionId());
                        result.put(businessKey, map);
                        break;
                    }
                }
            }
        }
        return result;
    }

    /**
     * 根据授权规则获取节点审批人
     *
     * @param auth 授权规则
     * @return 节点审批人
     */
    public List<XtzyUser> getUsersByAuth(WorkflowAuthVO auth) {
        XtzyUser sysUser = ShiroUtils.getUser();
        List<XtzyUser> users = new ArrayList<>();
        if (auth.getAuthType().equals("1")) {
            List<XtzyCompanyVO> companyList = new ArrayList<>();
            //todo : 需要再传参数
            companyList = companyService.getCompanyAndUsers(auth.getPathLevel(), auth.getRoleId(), 0, sysUser);
            for (XtzyCompanyVO company : companyList) {
                List<XtzyUser> userList = company.getUserList();
                if (userList != null && userList.size() > 0) {
                    for (XtzyUser xtzyUser : userList) {
                        users.add(xtzyUser);
                    }
                }
            }
        } else {
            users = companyService.getUserAndCompIdAndRoleId(auth.getCompId(), auth.getRoleId());
        }
        System.out.println(users.size());
        return users;
    }

    public WorkflowAuthVO convertAuth(WorkflowAuth auth) {
        WorkflowAuthVO vo = new WorkflowAuthVO();
        BeanUtils.copyBeanProp(vo, auth);
        return vo;
    }

    public List<Map<String, String>> convertXtzyUser(List<XtzyUser> users) {
        List<Map<String, String>> userList = new ArrayList<>();
        for (XtzyUser user : users) {
            Map<String, String> nextAuditUser = new HashMap<>();
            nextAuditUser.put("userId", user.getUserCode());
            nextAuditUser.put("userName", user.getUserDesc());
            nextAuditUser.put("deptEname", user.getExt1());
            nextAuditUser.put("deptCname", user.getExt2());
            nextAuditUser.put("roleEname", user.getExt3());
//            nextAuditUser.put("roleCname", auth.getRoleName());
            userList.add(nextAuditUser);
        }
        return userList;
    }

    public String businessKeysToString(List<String> businessKeys) {
        StringBuffer sb = new StringBuffer();
        if (businessKeys != null) {
            for (String key : businessKeys) {
                sb.append(key).append("##");
            }
        }
        return sb.toString();
    }

    public String[] stringTobusinessKeys(String businessKeys) {
        if (StringUtils.isEmpty(businessKeys)) {
            return new String[0];
        } else if (businessKeys.contains("##")) {
            return businessKeys.split("##");
        } else {
            return new String[]{businessKeys};
        }
    }


}
